/***************************************************************************
 *
 * This document contains confidential and proprietary information 
 * subject to non-disclosure agreements with AsiaInspection. This 
 * information shall not be distributed or copied without written 
 * permission from the AsiaInspection.
 *
 ***************************************************************************/
package concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/***************************************************************************
 *<PRE>
 *  Project Name    : com.allen
 *
 *  Package Name    : concurrent
 *
 *  File Name       : ExecutorServiceDemo.java
 *
 *  Creation Date   : Dec 25, 2018
 *
 *  Author          : Allen Zhang
 *
 *  Purpose         : 模拟多线程的电影下载器
 *
 *
 *  History         :
 *
 *</PRE>
 ***************************************************************************/

public class ExecutorServiceDemo {

    public static void main(String args[]) {
        ExecutorService pool = Executors.newFixedThreadPool(3);

        DownloadMovieTask mvStar = new DownloadMovieTask("Start Wars");
        DownloadMovieTask mvTitan = new DownloadMovieTask("Titanic");
        DownloadMovieTask mvDhxy = new DownloadMovieTask("大话西游");
        DownloadMovieTask mvPainter = new DownloadMovieTask("无双");
        DownloadMovieTask mvSpider = new DownloadMovieTask("Spider Man");

        List<Callable<Integer>> listOfCallable = new ArrayList<>();
        listOfCallable.add(mvDhxy);
        listOfCallable.add(mvTitan);
        listOfCallable.add(mvStar);
        listOfCallable.add(mvPainter);
        listOfCallable.add(mvSpider);

        try {
            List<Future<Integer>> results = pool.invokeAll(listOfCallable);
            results.stream().map(f -> {
                try {
                    System.out.println(" size: " + f.get() + " done.");
                    return f.get();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            });

        } catch (InterruptedException e) {
            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
        } finally {
            pool.shutdown();
        }
        System.out.println("所有下载完毕。");

    }
}
